package com.wondersgroup.fjzlpt.tbxt.service.impl.zh;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.tbxt.dao.inter.gyzjb.YyfsDao;
import com.wondersgroup.fjzlpt.tbxt.dao.inter.zh.ZhYbDao;
import com.wondersgroup.fjzlpt.tbxt.service.inter.zh.ZhYbService;

@Service
public class ZhYbServiceImpl implements ZhYbService {

	@Autowired
	private ZhYbDao zhYbDao;
	
	@Autowired
	private YyfsDao yyfsDao;
	
	/**
	 * 保存：综合医院-综合信息月报
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject save(Map<String, Object> map) {
		ResultObject ro = new ResultObject();
		String dataArr[] = map.get("NFYF").toString().split("-");
		map.put("NF", dataArr[0]);
		map.put("YF", dataArr[1]);
		
		String type = map.get("type").toString();
		if("add".equals(type)){
			this.insert(map, ro);
		}else{
			this.update(map, ro);
		}
		return (JSONObject)JSON.toJSON(ro);
	}
	
	/**
	 * 新增：综合医院-综合信息月报
	 * @param map
	 * @param ro
	 */
	private void insert(Map<String, Object> map, ResultObject ro){
		try {
			map.put("ID", UUID.randomUUID().toString());
			Date now = new Date();
			map.put("XZSJ", now);
			map.put("XGSJ", now);
			zhYbDao.insert(map);
			
			insertYyfs(map);
		} catch (Exception e) {
			ro.changeFaultState("综合信息月报保存失败！！");
			throw new RuntimeException("新增：综合医院-综合信息月报失败！！", e);
		}
	}
	
	/**
	 * 修改：综合医院-综合信息月报
	 * @param map
	 * @param ro
	 */
	private void update(Map<String, Object> map, ResultObject ro){
		try {
			map.put("XGSJ", new Date());
			zhYbDao.updateForPrimaryKey(map);
			//删除预约方式
			Map<String, Object> delMap = new HashMap<String, Object>();
			delMap.put("BBID", map.get("ID"));
			yyfsDao.deleteByBbid(delMap);
			//重新加入预约方式
			insertYyfs(map);
		} catch (Exception e) {
			ro.changeFaultState("综合信息月报修改失败！！");
			throw new RuntimeException("修改：综合医院-综合信息月报失败！！");
		}
	}
	
	/**
	 * 预约方式批处理
	 * @param map
	 */
	private void insertYyfs(Map<String, Object> map) {
		String YYFS = map.get("YYFS").toString();
		if(!StringUtils.isEmpty(YYFS)){
			List<Map> yyfsList = new ArrayList<Map>();
			String[] yyfsArr = YYFS.split(",");
			for (String yyfs : yyfsArr) {
				Map<String, Object> itemMap = new HashMap<String, Object>();
				itemMap.put("BBID", map.get("ID"));
				itemMap.put("YYFSID", yyfs);
				yyfsList.add(itemMap);
			}
			yyfsDao.insertBatch(yyfsList);
		}
	}
	
	/**
	 * 查询某机构填报记录
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryOne(Map<String, String> map) {
		JSONObject result = new JSONObject();
		try {
			result = zhYbDao.queryOne(map);
			if(result.isEmpty()){
				result.put("JGMC" , map.get("JGMC"));
			}
		} catch (Exception e) {
			result.put("errInfo","填报信息查询失败，请重试！");
			throw new RuntimeException("填报信息查询失败，请重试！", e);
		}
		return result;
	}
}
